<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>2.6 API Gateway | weiyonghua&#39;s Blog | YOUR_SUBTITLE</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="springclould">
    <meta name="description" content="2.6 API GatewayAPI Gateway是微服务架构中不可或缺的部分。API Gateway的定义以及存在的意义，Chris已经为大家描述过了，本文不再赘述，以下是链接：   中文版：http://dockone.io/article/482  英文版：https://www.nginx.com/blog/building-microservices-using-an-api-gate">
<meta name="keywords" content="springclould">
<meta property="og:type" content="article">
<meta property="og:title" content="2.6 API Gateway">
<meta property="og:url" content="weiyonghua's Blog/2017/07/03/2-6-API-Gateway/index.html">
<meta property="og:site_name" content="weiyonghua&#39;s Blog">
<meta property="og:description" content="2.6 API GatewayAPI Gateway是微服务架构中不可或缺的部分。API Gateway的定义以及存在的意义，Chris已经为大家描述过了，本文不再赘述，以下是链接：   中文版：http://dockone.io/article/482  英文版：https://www.nginx.com/blog/building-microservices-using-an-api-gate">
<meta property="og:image" content="weiyonghua%27s%20Blog/2017/07/03/2-6-API-Gateway/images/gateway.png">
<meta property="og:updated_time" content="2017-07-03T08:31:49.438Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="2.6 API Gateway">
<meta name="twitter:description" content="2.6 API GatewayAPI Gateway是微服务架构中不可或缺的部分。API Gateway的定义以及存在的意义，Chris已经为大家描述过了，本文不再赘述，以下是链接：   中文版：http://dockone.io/article/482  英文版：https://www.nginx.com/blog/building-microservices-using-an-api-gate">
<meta name="twitter:image" content="weiyonghua%27s%20Blog/2017/07/03/2-6-API-Gateway/images/gateway.png">
    
        <link rel="alternative" href="/atom.xml" title="weiyonghua&#39;s Blog" type="application/atom+xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.9">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">weiyonghua</h5>
          <a href="mailto:weiyonghua16@gmail.com" title="weiyonghua16@gmail.com" class="mail">weiyonghua16@gmail.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/1135831485" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://t.qq.com/fy1135831485" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">2.6 API Gateway</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">2.6 API Gateway</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-07-03T08:26:00.000Z" itemprop="datePublished" class="page-time">
  2017-07-03
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/springclould专栏/">springclould专栏</a></li></ul>

            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#2-6-API-Gateway"><span class="post-toc-number">1.</span> <span class="post-toc-text">2.6 API Gateway</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#准备工作"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">准备工作</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Zuul代码示例"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">Zuul代码示例</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#测试"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">测试</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#自定义路径"><span class="post-toc-number">1.4.</span> <span class="post-toc-text">自定义路径</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#如何忽略某些服务"><span class="post-toc-number">1.5.</span> <span class="post-toc-text">如何忽略某些服务</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#准备工作-1"><span class="post-toc-number">1.5.1.</span> <span class="post-toc-text">准备工作</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#使用Zuul不使用Eureka"><span class="post-toc-number">1.6.</span> <span class="post-toc-text">使用Zuul不使用Eureka</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#其他使用"><span class="post-toc-number">1.7.</span> <span class="post-toc-text">其他使用</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#同类软件"><span class="post-toc-number">1.8.</span> <span class="post-toc-text">同类软件</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#参考文档"><span class="post-toc-number">1.9.</span> <span class="post-toc-text">参考文档</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-2-6-API-Gateway"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">2.6 API Gateway</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年07月03日 16:26" datetime="2017-07-03T08:26:00.000Z"  itemprop="datePublished">2017-07-03</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/springclould专栏/">springclould专栏</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h1 id="2-6-API-Gateway"><a href="#2-6-API-Gateway" class="headerlink" title="2.6 API Gateway"></a>2.6 API Gateway</h1><p>API Gateway是微服务架构中不可或缺的部分。API Gateway的定义以及存在的意义，Chris已经为大家描述过了，本文不再赘述，以下是链接：</p>
<blockquote>
<p> 中文版：<a href="http://dockone.io/article/482" target="_blank" rel="external">http://dockone.io/article/482</a></p>
<p> 英文版：<a href="https://www.nginx.com/blog/building-microservices-using-an-api-gateway/" target="_blank" rel="external">https://www.nginx.com/blog/building-microservices-using-an-api-gateway/</a></p>
</blockquote>
<p>使用API Gateway后，客户端和微服务之间的网络图变成下图：</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="images/gateway.png" alt="API" title="">
                </div>
                <div class="image-caption">API</div>
            </figure>
<p>通过API Gateway，可以统一向外部系统提供REST API。Spring Cloud中使用Zuul作为API Gateway。Zuul提供了动态路由、监控、回退、安全等功能。</p>
<p>下面我们进入Zuul的学习：</p>
<h2 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h2><ul>
<li>为了更贴近生产，我们首先配置Host</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">127.0.0.1 gateway</div></pre></td></tr></table></figure>
<ul>
<li>启动服务：microservice-discovery-eureka</li>
<li>启动服务：microservice-provider-user</li>
</ul>
<h2 id="Zuul代码示例"><a href="#Zuul代码示例" class="headerlink" title="Zuul代码示例"></a>Zuul代码示例</h2><p>创建Maven项目，在pom.xml中添加如下内容：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</div><div class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></div><div class="line">  <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>microservice-api-gateway<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">packaging</span>&gt;</span>jar<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span></div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">parent</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.itmuch.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-microservice-study<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">parent</span>&gt;</span></div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-zuul<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line"></div><div class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-eureka<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></div></pre></td></tr></table></figure>
<p>启动类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 使用<span class="doctag">@EnableZuulProxy</span>注解激活zuul。</div><div class="line"> * 跟进该注解可以看到该注解整合了<span class="doctag">@EnableCircuitBreaker</span>、<span class="doctag">@EnableDiscoveryClient</span>，是个组合注解，目的是简化配置。</div><div class="line"> * <span class="doctag">@author</span> eacdy</div><div class="line"> */</div><div class="line"><span class="meta">@SpringBootApplication</span></div><div class="line"><span class="meta">@EnableZuulProxy</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ZuulApiGatewayApplication</span> </span>&#123;</div><div class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">    SpringApplication.run(ZuulApiGatewayApplication.class, args);</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>配置文件：application.yml</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="attr">spring:</span></div><div class="line"><span class="attr">  application:</span></div><div class="line"><span class="attr">    name:</span> <span class="string">microservice-api-gateway</span></div><div class="line"><span class="attr">server:</span></div><div class="line"><span class="attr">  port:</span> <span class="number">8050</span></div><div class="line"><span class="attr">eureka:</span></div><div class="line"><span class="attr">  instance:</span></div><div class="line"><span class="attr">    hostname:</span> <span class="string">gateway</span></div><div class="line"><span class="attr">  client:</span></div><div class="line"><span class="attr">    serviceUrl:</span></div><div class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://discovery:8761/eureka/</span></div></pre></td></tr></table></figure>
<p>这样，一个简单的API Gateway就完成了。</p>
<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><p>启动microservice-api-gateway项目。还记得我们之前访问通过<a href="http://localhost:8000/1" target="_blank" rel="external">http://localhost:8000/1</a>去访问microservice-provider-user服务中id=1的用户信息吗？</p>
<p>我们现在访问<a href="http://localhost:8050/microservice-provider-user/1" target="_blank" rel="external">http://localhost:8050/microservice-provider-user/1</a>试试。会惊人地看到：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123;<span class="attr">"id"</span>:<span class="number">1</span>,<span class="attr">"username"</span>:<span class="string">"Tom"</span>,<span class="attr">"age"</span>:<span class="number">12</span>&#125;</div></pre></td></tr></table></figure>
<p>这不正是microservice-provider-user服务中id=1的用户信息吗？</p>
<p>所以我们可以总结出规律：访问</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">http://GATEWAY:GATEWAY_PORT/想要访问的Eureka服务id的小写/**</div></pre></td></tr></table></figure>
<p>，将会访问到</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">http://想要访问的Eureka服务id的小写:该服务端口/**</div></pre></td></tr></table></figure>
<h2 id="自定义路径"><a href="#自定义路径" class="headerlink" title="自定义路径"></a>自定义路径</h2><p>上文我们已经完成了通过API Gateway去访问微服务的目的，是通过</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">http://GATEWAY:GATEWAY_PORT/想要访问的Eureka服务id的小写/**</div></pre></td></tr></table></figure>
<p>的形式访问的，那么如果我们想自定义在API Gateway中的路径呢？譬如想使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">http://localhost:8050/user/1</div></pre></td></tr></table></figure>
<p>就能够将请求路由到<a href="http://localhost:8000/1呢？" target="_blank" rel="external">http://localhost:8000/1呢？</a></p>
<p>只需要做一点小小的配置即可：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="attr">spring:</span></div><div class="line"><span class="attr">  application:</span></div><div class="line"><span class="attr">    name:</span> <span class="string">microservice-api-gateway</span></div><div class="line"><span class="attr">server:</span></div><div class="line"><span class="attr">  port:</span> <span class="number">8050</span></div><div class="line"><span class="attr">eureka:</span></div><div class="line"><span class="attr">  instance:</span></div><div class="line"><span class="attr">    hostname:</span> <span class="string">gateway</span></div><div class="line"><span class="attr">  client:</span></div><div class="line"><span class="attr">    serviceUrl:</span></div><div class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://discovery:8761/eureka/</span></div><div class="line"><span class="comment"># 下面整个树都非必须，如果不配置，将默认使用 http://GATEWAY:GATEWAY_PORT/想要访问的Eureka服务id的小写/** 路由到：http://想要访问的Eureka服务id的小写:该服务端口/**</span></div><div class="line"><span class="attr">zuul:</span></div><div class="line"><span class="attr">  routes:</span></div><div class="line"><span class="attr">    user:</span>                                               <span class="comment"># 可以随便写，在zuul上面唯一即可；当这里的值 = service-id时，service-id可以不写。</span></div><div class="line"><span class="attr">      path:</span> <span class="string">/user/**</span>                                    <span class="comment"># 想要映射到的路径</span></div><div class="line"><span class="attr">      service-id:</span> <span class="string">microservice-provider-user</span>            <span class="comment"># Eureka中的serviceId</span></div></pre></td></tr></table></figure>
<h2 id="如何忽略某些服务"><a href="#如何忽略某些服务" class="headerlink" title="如何忽略某些服务"></a>如何忽略某些服务</h2><h3 id="准备工作-1"><a href="#准备工作-1" class="headerlink" title="准备工作"></a>准备工作</h3><ol>
<li>启动服务：microservice-discovery-eureka</li>
<li>启动服务：microservice-provider-user</li>
<li>启动服务：microservice-consumer-movie-ribbon</li>
</ol>
<p>如果我们现在只想将microservice-consumer-movie-ribbon服务暴露给外部，microservice-provider-user不想暴露，那么应该怎么办呢？</p>
<p>依然只是一点小小的配置即可：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="attr">spring:</span></div><div class="line"><span class="attr">  application:</span></div><div class="line"><span class="attr">    name:</span> <span class="string">microservice-api-gateway</span></div><div class="line"><span class="attr">server:</span></div><div class="line"><span class="attr">  port:</span> <span class="number">8050</span></div><div class="line"><span class="attr">eureka:</span></div><div class="line"><span class="attr">  instance:</span></div><div class="line"><span class="attr">    hostname:</span> <span class="string">gateway</span></div><div class="line"><span class="attr">  client:</span></div><div class="line"><span class="attr">    serviceUrl:</span></div><div class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://discovery:8761/eureka/</span></div><div class="line"><span class="attr">zuul:</span></div><div class="line"><span class="attr">  ignored-services:</span> <span class="string">microservice-provider-user</span>          <span class="comment"># 需要忽视的服务(配置后将不会被路由)</span></div><div class="line"><span class="attr">  routes:</span></div><div class="line"><span class="attr">    movie:</span>                                              <span class="comment"># 可以随便写，在zuul上面唯一即可；当这里的值 = service-id时，service-id可以不写。</span></div><div class="line"><span class="attr">      path:</span> <span class="string">/movie/**</span>                                   <span class="comment"># 想要映射到的路径</span></div><div class="line"><span class="attr">      service-id:</span> <span class="string">microservice-consumer-movie-ribbon-with-hystrix</span>    <span class="comment"># Eureka中的serviceId</span></div></pre></td></tr></table></figure>
<p>这样microservice-provider-user服务就不会被路由，microservice-consumer-movie-ribbon服务则会被路由。</p>
<p>测试结果：</p>
<table>
<thead>
<tr>
<th>URL</th>
<th>结果</th>
<th>备注</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://localhost:8050/microservice-provider-user/1" target="_blank" rel="external">http://localhost:8050/microservice-provider-user/1</a></td>
<td>404</td>
<td>说明microservice-provider-user未被路由</td>
</tr>
<tr>
<td><a href="http://localhost:8050/movie/ribbon/1" target="_blank" rel="external">http://localhost:8050/movie/ribbon/1</a></td>
<td>{“id”:1,”username”:”Tom”,”age”:12}</td>
<td>说明microservice-consumer-movie-ribbon被路由了。</td>
</tr>
</tbody>
</table>
<h2 id="使用Zuul不使用Eureka"><a href="#使用Zuul不使用Eureka" class="headerlink" title="使用Zuul不使用Eureka"></a>使用Zuul不使用Eureka</h2><p>Zuul并不依赖Eureka，可以脱离Eureka运行，此时需要配置</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="attr">spring:</span></div><div class="line"><span class="attr">  application:</span></div><div class="line"><span class="attr">    name:</span> <span class="string">microservice-api-gateway</span></div><div class="line"><span class="attr">server:</span></div><div class="line"><span class="attr">  port:</span> <span class="number">8050</span></div><div class="line"><span class="attr">zuul:</span></div><div class="line"><span class="attr">  routes:</span></div><div class="line"><span class="attr">    movie:</span>                                              <span class="comment"># 可以随便写</span></div><div class="line"><span class="attr">      path:</span> <span class="string">/user/**</span></div><div class="line"><span class="attr">      url:</span> <span class="attr">http://localhost:8000/</span>                       <span class="comment"># path路由到的地址，也就是访问http://localhost:8050/user/**会路由到http://localhost:8000/**</span></div></pre></td></tr></table></figure>
<p>我们可尝试访问<a href="http://localhost:8050/user/1" target="_blank" rel="external">http://localhost:8050/user/1</a> ，会发现被路由到了<a href="http://localhost:8000/1" target="_blank" rel="external">http://localhost:8000/1</a> 。不过在大多数情况下，笔者并不建议这么做，因为得手动大量地配置URL，不是很方便。</p>
<h2 id="其他使用"><a href="#其他使用" class="headerlink" title="其他使用"></a>其他使用</h2><p>Zuul还支持更多的特性、更多的配置项甚至是定制开发，具体还请读者自行发掘。</p>
<h2 id="同类软件"><a href="#同类软件" class="headerlink" title="同类软件"></a>同类软件</h2><p>Zuul只是API Gateway的一种实现，可作为API Gateway的软件有很多，譬如Nginx Plus、Kong等等，本文不做赘述了。</p>
<h2 id="参考文档"><a href="#参考文档" class="headerlink" title="参考文档"></a>参考文档</h2><p><a href="https://www.nginx.com/blog/building-microservices-using-an-api-gateway/" target="_blank" rel="external">https://www.nginx.com/blog/building-microservices-using-an-api-gateway/</a><br><a href="http://microservices.io/patterns/apigateway.html" target="_blank" rel="external">http://microservices.io/patterns/apigateway.html</a></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-07-03T08:31:49.438Z" itemprop="dateUpdated">2017年7月3日 16:31</time>
</span><br>


        这里写留言或版权声明：<a href="/2017/07/03/2-6-API-Gateway/" target="_blank" rel="external">weiyonghua&#39;s Blog/2017/07/03/2-6-API-Gateway/</a>
    </div>
    <footer>
        <a href="weiyonghua's Blog">
            <img src="/img/avatar.jpg" alt="weiyonghua">
            weiyonghua
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/springclould/">springclould</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=weiyonghua's Blog/2017/07/03/2-6-API-Gateway/&title=《2.6 API Gateway》 — weiyonghua's Blog&pic=weiyonghua's Blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=weiyonghua's Blog/2017/07/03/2-6-API-Gateway/&title=《2.6 API Gateway》 — weiyonghua's Blog&source=YOUR_DESC" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=weiyonghua's Blog/2017/07/03/2-6-API-Gateway/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《2.6 API Gateway》 — weiyonghua's Blog&url=weiyonghua's Blog/2017/07/03/2-6-API-Gateway/&via=weiyonghua's Blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=weiyonghua's Blog/2017/07/03/2-6-API-Gateway/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between flex-row-reverse">
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/07/03/2-4-3-Turbine/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">2.4.3 Turbine</h4>
      </a>
    </div>
  
</nav>



    

<div class="comments" id="comments">
    <div class="ds-thread" data-thread-key="2-6-API-Gateway" data-title="2.6 API Gateway" data-url="weiyonghua's Blog/2017/07/03/2-6-API-Gateway/"></div>
</div>
<script>
lazyScripts.push('//cdn.bootcss.com/marked/0.3.6/marked.min.js');

var duoshuoQuery = {short_name:'ysblog', theme: 'none'};
lazyScripts.push('/js/embed.min.js?v=1.4.9');


</script>







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.jpg" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a></span>
            <span>weiyonghua's Blog &copy; 2015 - 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=weiyonghua's Blog/2017/07/03/2-6-API-Gateway/&title=《2.6 API Gateway》 — weiyonghua's Blog&pic=weiyonghua's Blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=weiyonghua's Blog/2017/07/03/2-6-API-Gateway/&title=《2.6 API Gateway》 — weiyonghua's Blog&source=YOUR_DESC" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=weiyonghua's Blog/2017/07/03/2-6-API-Gateway/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《2.6 API Gateway》 — weiyonghua's Blog&url=weiyonghua's Blog/2017/07/03/2-6-API-Gateway/&via=weiyonghua's Blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=weiyonghua's Blog/2017/07/03/2-6-API-Gateway/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



</script>

<script src="/js/main.min.js?v=1.4.9"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.9" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
